import Vue from 'vue'
import VueRouter from 'vue-router'

const Home = () => import('@/pages/Home')
const Detail = () => import('@/pages/Detail')
const Category = () => import('@/pages/Category')
const Cart = () => import('@/pages/Cart')
const Profile = () => import('@/pages/Profile')

// 处理重复点击同一路由报错
// push方法
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch((err) => err)
}
// replace方法
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (location) {
  return originalReplace.call(this, location).catch((err) => err)
}

Vue.use(VueRouter)

const routes = [
  { path: '', redirect: '/home' },
  { path: '/home', component: Home },
  { path: '/detail/:id', component: Detail },
  { path: '/category', component: Category },
  { path: '/cart', component: Cart },
  { path: '/profile', component: Profile }
]

export default new VueRouter({
  routes
})
